package com.ubird.particle;

import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;

public abstract class Particles {

    private List<ParticleNode> particleNode;

    public Particles() {
        particleNode = getParticleNodes();
    }

    public Particles(int x, int y) {
        particleNode = getParticleNodes(x, y);
    }

    protected abstract List<ParticleNode> getParticleNodes();

    protected abstract List<ParticleNode> getParticleNodes(int x, int y);

    public static Particles getInstance(int x, int y) {
        return new Particles1(x, y);
    }

    public void init(int x, int y) {
        if (particleNode != null) {
            for (ParticleNode node : particleNode) {
                node.init(x, y);
            }
        }
    }

    public void init(int y) {
        if (particleNode != null) {
            for (ParticleNode node : particleNode) {
                node.initY(y);
            }
        }
    }

    public void update(int time) {
        if (particleNode != null) {
            for (ParticleNode node : particleNode) {
                node.update(time);
            }
        }
    }

    public void draw(Graphics g) {
        if (particleNode != null) {
            for (ParticleNode node : particleNode) {
                node.draw(g);
            }
        }
    }

    static class Particles1 extends Particles {

        private final float ANGLE = (float) (2 * Math.PI);
        private static Random random = new Random();

        public Particles1(int x, int y) {
            super(x, y);
        }

        protected List<ParticleNode> getParticleNodes() {
            return getParticleNodes(0, 0);
        }

        protected List<ParticleNode> getParticleNodes(int x, int y) {
            int num = 400;
            int v0 = 1;
            int rangX = 400;
            int rangY = 20;
            List<ParticleNode> list = new ArrayList<ParticleNode>(num);
            Image texture = null;
            try {
                texture = ImageIO.read(getClass().getClassLoader().getResource("com/ubird/ui/res/Tetris.png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < num; i++) {
                int fixX = random.nextInt(rangX) - rangX / 2;
                int fixY = 0;// random.nextInt(rangY) - rangY/2;
                float ang = ANGLE * i / num;
                float vx = (float) (v0 * Math.sin(ang));
                float vy = (float) (v0 * Math.cos(ang));
                // vy = Math.abs(vy);
                vx = 0;
                int size = 10 + random.nextInt(10);
                int life = 35;
                int delay = random.nextInt(life);
                ParticleNode particleNode2 = new ParticleNode(x + fixX, y + fixY, size, size, vx, vy, ANGLE / 240, 0,
                        vy / 80, life, delay, ParticleNode.TYPE_ONCE_AND_NOT_DISPLAY_FIRST);
                particleNode2.setTexture(texture);
                list.add(particleNode2);
            }
            return list;
        }
    }
}
